#ifndef NETWORKCONFIG_H
# define NETWORKCONFIG_H

#include "linuxos.h"

#include "fileutils.h"
#include "stringutils.h"

typedef struct _ConfigInterface {
    bool isAuto;
    char iface[100];
    char ipType[100];   // inet or inet6
    char workType[100]; // dhcp or manual or static
    GSList *properties; // ConfigInterfaceProperty
} ConfigInterface;

//Config: Attribute-value
typedef struct _ConfigInterfaceProperty {
    char key[100];
    char value[100];
} ConfigInterfaceProperty;


#define NC_PROPERTY_FREE_ALL_CLEAN(sl) GS_FREE_ALL_CLEAN(sl)
#define NC_CONFIG_INTERFACE_FREE_ALL_CLEAN(sl) GS_FREE_ALL_CLEAN(sl)

#define NC_CIS_FREE_ALL_CLEAN(cis) GS_FREE_ALL_CLEAN(cis)
#define NC_CIP_FREE_ALL_CLEAN(ci) NC_PROPERTY_FREE_ALL_CLEAN(((ConfigInterface*)ci)->properties)


// 格式化输出 - 
void createFormat(char *buffer, char value, int len);
void config_property_iterator(gpointer item, gpointer prefix);

// 复制属性
ConfigInterfaceProperty *createConfigInterfacePropertyCopy(const ConfigInterfaceProperty *const_property);
// 复制接口状态/属性
ConfigInterface * createConfigInterfaceCopy(const ConfigInterface *const_interface);

/**
 * setConfigInterface 初始化
 * *interface 接口对象
 * isauto 是否自动开启
 * iface 接口名
 * iptype 类型 inet/inet6/conf(hostapd)
 * worktype 工作模式 dhcp/manual/static/auto
 * properties 
 */
bool setConfigInterface(ConfigInterface *interface, 
                        bool isauto, char iface[100], 
                        char iptype[100], char worktype[100], 
                        GSList *properties);

// ------------------------------------ Config File Low Api

// configInterfaceAppend 添加一个接口及属性值列表
static bool writeConfig(FILE *fp, const char *iface, const bool isAuto, const char *ipv4, const char *type, const GSList *properties);
static bool writeConfigInterface(FILE *fp, const ConfigInterface *interface);

// ------------------------------------ Network Config: Wrap FilePath
// 配置文件是否存在
bool NetworkConfigExists(const char *path);

// 获取所有接口名
bool NetworkConfigInterfaceNames(const char *path, GSList **list);
// 获取所有自动配置接口名
bool NetworkConfigInterfaceAutoNames(const char *path, GSList **list);

// 判断接口是否存在
bool NetworkConfigInterfaceExists(const char *path, const char *iface);

// 查询接口是否为auto
bool NetworkConfigInterfaceIsAuto(const char *path, char *iface);

// 接口对象状态 - 列表值: iface br0 inet static
bool NetworkConfigInterfaceDetails(const char *path, char *iface, GSList **list);

// ------------------------------------ Network Config: ConfigInterface / ConfigInterfaceProperty

// 获取所有接口配置
bool NetworkConfigsWithPath(const char *path, GSList **list);

// 寻找配置文件中接口的配置状态信息
bool NetworkConfigInterface(const char *path, char *iface, ConfigInterface *interface);

// 获取配置文件的中指定接口节点的属性
bool NetworkConfigInterfaceSectionProperties(const char *path, char *section, GSList **list);

// ------------------------------------ Network Config: Writer to File(Save Config)

// 将接口信息写入配置文件
bool NetworkConfigInterfaceWriter(const char *path, const GSList *interfaces);

// ------------------------------------ Network Config: Append
// 在配置文件中追加一个接口 - 接口可能含属性
bool NetworkConfigAppendConfigInterface(const char *path, ConfigInterface *interface);

// 在配置文件中追加多个接口 - 接口可能含属性
bool NetworkConfigAppendConfigInterfaces(const char *path, GSList *interfaces);

// ------------------------------------ Network Config: Modify
// 在配置文件中修改一个接口信息
bool NetworkConfigModifyConfigInterface(const char *path, const ConfigInterface *modify_interface);

// ------------------------------------ Network Config: Insert
// 配置文件中插入一个接口 - 如果接口已存在，将返回失败
bool NetworkConfigInsertConfigInterface(const char *path, const ConfigInterface *interface);
bool NetworkConfigReplaceConfigInterface(const char *path, const ConfigInterface *interface);

// ------------------------------------ Network Config: Remove
// 配置文件中移除一个接口
bool NetworkConfigRemoveConfigInterface(const char *path , const char *iface);

#endif